<template>
  <div class="app-container">
    <div class="operation-crumb clearfix">
      <el-button type="primary">增加分类</el-button>
    </div>
    <div class="table-container">
      <el-table
        ref="goodsCateTable"
        style="width: 100%"
        :data="list"
        :header-cell-class-name="tableHeaderClassName"
        v-loading="listLoading"
        @expand-change="expandChange"
        border
      >
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="下级" width="200" align="center" type="expand">
          <template slot-scope="scope">
            <el-table
              border
              :data="scope.row.children"
              style="width: calc(100% - 47px);"
              v-loading="listLoading"
            >
              <el-table-column label="编号" width="100" align="center">
                <template slot-scope="scope">{{scope.row.id}}</template>
              </el-table-column>
              <el-table-column label="分类名称" align="center">
                <template slot-scope="scope">{{scope.row.name}}</template>
              </el-table-column>
              <el-table-column label="级别" width="100" align="center">
                <template slot-scope="scope">{{scope.row.level | levelFilter}}</template>
              </el-table-column>
              <el-table-column label="是否显示" width="100" align="center">
                <template slot-scope="scope">
                  <el-switch
                    @change="handleShowStatusChange(scope.$index, scope.row)"
                    :active-value="1"
                    :inactive-value="0"
                    v-model="scope.row.showStatus"
                  ></el-switch>
                </template>
              </el-table-column>
              <el-table-column label="排序" width="100" align="center">
                <template slot-scope="scope">{{scope.row.sort }}</template>
              </el-table-column>
              <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                  <el-button size="mini" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row,true)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="分类名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="级别" width="100" align="center">
          <template slot-scope="scope">{{scope.row.level | levelFilter}}</template>
        </el-table-column>
        <el-table-column label="是否显示" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleShowStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.showStatus"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import api from "@/http";

export default {
  name: "goodsCateList",
  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 5
      },
      parentId: 0
    };
  },
  created() {
    this.resetParentId();
    this.getList();
  },
  watch: {
    $route(route) {
      this.resetParentId();
      this.getList();
    }
  },
  methods: {
    //展开下级
    expandChange(data) {
      this.listLoading = true;
      this.$api.category
        .fetchListSon(data.id)
        .then(response => {
          this.listLoading = false;
          data["children"] = response.data;
        });
    },
    // 表格背景色
    tableHeaderClassName({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return "header-bg";
      }

      return "";
    },
    resetParentId() {
      this.listQuery.pageNum = 1;
      if (this.$route.query.parentId != null) {
        this.parentId = this.$route.query.parentId;
      } else {
        this.parentId = 0;
      }
    },
    handleAddGoodsCate() {
      this.$router.push("/goodsCategory/add");
    },
    getList() {
      this.listLoading = true;
      this.$api.category
        .fetchList(this.parentId, this.listQuery)
        .then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleShowStatusChange(index, row) {
      let ids = [];
      ids.push(row.id);
      let data = { ids: ids, showStatus: row.showStatus };
      this.$api.category.updateShowStatus(data).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    handleUpdate(index, row) {
      this.$router.push({
        path: "/goodsCategory/update",
        query: { id: row.id }
      });
    },
    handleDelete(index, row, isSon) {
      this.$confirm("是否要删除该分类", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$api.category.updateDeleteStatus(row.id).then(response => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000
          });
          if (isSon) {
            this.listLoading = true;
            this.$api.category
              .fetchListSon(row.parentId)
              .then(response => {
                this.listLoading = false;
                let parent_row=this.list.find(c=>c.id==row.parentId)
                parent_row["children"] = response.data;
              });
          } else {
            this.getList();
          }
        });
      });
    }
  },
  filters: {
    levelFilter(value) {
      if (value === 0) {
        return "一级";
      } else if (value === 1) {
        return "二级";
      }
    },
    disableNextLevel(value) {
      if (value === 0) {
        return false;
      } else {
        return true;
      }
    }
  }
};
</script>

<style scoped>
</style>
